/**
 * 
 */
package ru.cargobroker.workbench.registry.services.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import ru.cargobroker.workbench.registry.domain.PerspectiveDescriptor;
import ru.cargobroker.workbench.registry.services.PerspectiveRegistry;

/**
 * @author M.Frendiy
 * @since 0.1
 *
 */
@Component
public class InMemoryPerspectiveRegistry implements PerspectiveRegistry {
	private static final Logger LOG = Logger.getLogger(InMemoryPerspectiveRegistry.class);
	
	private List<PerspectiveDescriptor> dataStore = new ArrayList<PerspectiveDescriptor>();
	
	
	/* (non-Javadoc)
	 * @see ru.cargobroker.workbench.registry.services.PerspectiveRegistry#getPerspectives()
	 */
	@Override
	public List<PerspectiveDescriptor> getPerspectives() {
		return Collections.unmodifiableList(dataStore);
	}

	/* (non-Javadoc)
	 * @see ru.cargobroker.workbench.registry.services.PerspectiveRegistry#getPerspectiveById(java.lang.String)
	 */
	@Override
	public PerspectiveDescriptor findPerspectiveById(String perspectiveId) {
		Assert.notNull(perspectiveId);
		
		for (PerspectiveDescriptor descriptor : dataStore) {
			if (descriptor.getId().equals(perspectiveId)) {
				return descriptor;
			}
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see ru.cargobroker.workbench.registry.services.PerspectiveRegistry#addPerspective(ru.cargobroker.workbench.registry.domain.PerspectiveDescriptor)
	 */
	@Override
	public void addPerspective(PerspectiveDescriptor desc) {
		Assert.notNull(desc);
		
		if (findPerspectiveById(desc.getId()) == null) {
			dataStore.add(desc);
			LOG.info("Added Perspective with id [" + desc.getId() + "]");
		} else {
			LOG.warn("Perspective with id [" + desc.getId() + "] already added");
		}
	}
}
